package com.aoxin.controller.client;

import com.aoxin.model.SlfSystemMessage;
import com.aoxin.service.inter.course.webCourse.SystemMessageService;
import com.aoxin.util.AjaxRes;
import com.aoxin.util.Const;
import com.aoxin.util.ThreadLocalUtil;
import com.aoxin.util.enumerate.OperateType;
import com.aoxin.util.enumerate.UserType;
import com.aoxin.util.log.LogInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/web/userSystemMessage/")
public class UserSystemMessageController {

    @Autowired
    SystemMessageService systemMessageService;


    /**
     * 查询某用户消息
     * @param res
     * @return
     */
    @RequestMapping("getUserMessage")
    @LogInfo(content = "系统消息",operateType = OperateType.QUERY,userType=UserType.USER)
    public AjaxRes getUserMessage( AjaxRes res){
        Integer userId = ThreadLocalUtil.getUser().getUserId();
        System.out.println(userId);
        if(userId != null){
            List<SlfSystemMessage> slfSystemMessageList = systemMessageService.getUserSystemMessage(userId);
            res.setSucceed(slfSystemMessageList,"getUserMessage success!");
        }else {
            res.setFailMsg("userId is null");
        }
        return res;
    }

    /**
     * 批量删除某用户信息
     * @param messageIds
     * @param res
     * @return
     */
    @RequestMapping("deleteUserMessage")
    @LogInfo(content = "系统消息",operateType = OperateType.DELETE,userType=UserType.USER)
    public AjaxRes deleteUserMessage(List<Integer> messageIds,AjaxRes res){
        if(messageIds != null && messageIds.size() > 0){
            Integer result = systemMessageService.deleteUserSystemMessage(messageIds);
            if(result != 0){
                res.setSucceedMsg("deleteUserMessage success!");
            }else {
                res.setFailMsg("deleteUserMessage Fail!");
            }
        }else {
            res.setFailMsg("messageIds is null!");
        }
        return res;
    }

    /**
     * 读消息
     * @param messageId
     * @param ajaxRes
     * @return
     */
    @RequestMapping("readMessage")
    @LogInfo(content = "阅读消息",operateType = OperateType.DELETE,userType=UserType.USER)
    public AjaxRes readMessage(Integer messageId,AjaxRes ajaxRes){
        try {
            SlfSystemMessage slfSystemMessage = new SlfSystemMessage();
            slfSystemMessage.setMessageId(messageId);
            slfSystemMessage.setIsRead(1);
            Integer result = this.systemMessageService.updateSystemMessage(slfSystemMessage);
            if(result != 0){
                ajaxRes.setSucceedMsg(Const.UPDATE_SUCCEED);
            }
        }catch (Exception e){
            ajaxRes.setFailMsg(e.getMessage());
        }
        return ajaxRes;
    }

}
